import { mockLogin, mockGetUserInfo, mockLogout } from './auth'
import { filterUsers, getUserById, users, roles } from './users'
import { 
  filterOrders, 
  getOrderById, 
  updateOrderStatusById, 
  handleRefund, 
  completeRefund, 
  updateOrderRemarkById,
  orders
} from './orders'
import dayjs from 'dayjs'

// 定义响应接口
interface MockResponse {
  code: number
  data: any
  message: string
}

// 模拟请求延迟
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

// 模拟API请求
export async function mockRequest(url: string, method: string, data?: any, params?: any): Promise<MockResponse> {
  // 模拟网络延迟
  await delay(500)
  
  // 认证相关接口
  if (url === '/auth/login' && method.toLowerCase() === 'post') {
    return mockLogin(data.username, data.password)
  }
  
  if (url === '/auth/user' && method.toLowerCase() === 'get') {
    return mockGetUserInfo()
  }
  
  if (url === '/auth/logout' && method.toLowerCase() === 'post') {
    return mockLogout()
  }
  
  // 用户管理相关接口
  if (url === '/users' && method.toLowerCase() === 'get') {
    return {
      code: 0,
      data: filterUsers(params || {}),
      message: '获取成功'
    }
  }
  
  if (url === '/roles' && method.toLowerCase() === 'get') {
    return {
      code: 0,
      data: roles,
      message: '获取成功'
    }
  }
  
  if (url.match(/^\/users\/\w+$/) && method.toLowerCase() === 'get') {
    const id = url.split('/').pop() || ''
    const user = getUserById(id)
    
    if (user) {
      return {
        code: 0,
        data: user,
        message: '获取成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '用户不存在'
    }
  }
  
  if (url === '/users' && method.toLowerCase() === 'post') {
    // 模拟创建用户
    const newUser = {
      ...data,
      id: String(users.length + 1),
      avatar: 'https://tdesign.gtimg.com/site/avatar.jpg',
      createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
    }
    
    users.push(newUser)
    
    return {
      code: 0,
      data: newUser,
      message: '创建成功'
    }
  }
  
  if (url.match(/^\/users\/\w+$/) && method.toLowerCase() === 'put') {
    const id = url.split('/').pop() || ''
    const index = users.findIndex(user => user.id === id)
    
    if (index !== -1) {
      // 更新用户信息，但保留敏感字段
      users[index] = {
        ...users[index],
        ...data,
        id: users[index].id,
        password: users[index].password
      }
      
      return {
        code: 0,
        data: users[index],
        message: '更新成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '用户不存在'
    }
  }
  
  if (url.match(/^\/users\/\w+$/) && method.toLowerCase() === 'delete') {
    const id = url.split('/').pop() || ''
    const index = users.findIndex(user => user.id === id)
    
    if (index !== -1) {
      const deletedUser = users.splice(index, 1)[0]
      
      return {
        code: 0,
        data: null,
        message: '删除成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '用户不存在'
    }
  }
  
  if (url.match(/^\/users\/\w+\/status$/) && method.toLowerCase() === 'patch') {
    const id = url.split('/')[2]
    const index = users.findIndex(user => user.id === id)
    
    if (index !== -1) {
      users[index].status = data.status
      
      return {
        code: 0,
        data: users[index],
        message: '状态更新成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '用户不存在'
    }
  }
  
  if (url.match(/^\/users\/\w+\/reset-password$/) && method.toLowerCase() === 'post') {
    const id = url.split('/')[2]
    const index = users.findIndex(user => user.id === id)
    
    if (index !== -1) {
      const newPassword = '123456'
      users[index].password = newPassword
      
      return {
        code: 0,
        data: { password: newPassword },
        message: '密码重置成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '用户不存在'
    }
  }
  
  // 订单管理相关接口
  if (url === '/orders' && method.toLowerCase() === 'get') {
    return {
      code: 0,
      data: filterOrders(params || {}),
      message: '获取成功'
    }
  }
  
  if (url.match(/^\/orders\/\w+$/) && method.toLowerCase() === 'get') {
    const id = url.split('/').pop() || ''
    const order = getOrderById(id)
    
    if (order) {
      return {
        code: 0,
        data: order,
        message: '获取成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '订单不存在'
    }
  }
  
  if (url.match(/^\/orders\/\w+\/status$/) && method.toLowerCase() === 'patch') {
    const id = url.split('/')[2]
    const order = updateOrderStatusById(id, data.status, data.reason)
    
    if (order) {
      return {
        code: 0,
        data: order,
        message: '状态更新成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '订单不存在'
    }
  }
  
  if (url.match(/^\/orders\/\w+\/refund$/) && method.toLowerCase() === 'post') {
    const id = url.split('/')[2]
    const order = handleRefund(id, data.reason, data.amount)
    
    if (order) {
      return {
        code: 0,
        data: order,
        message: '退款申请提交成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '订单不存在'
    }
  }
  
  if (url.match(/^\/orders\/\w+\/refund\/confirm$/) && method.toLowerCase() === 'post') {
    const id = url.split('/')[2]
    const order = completeRefund(id)
    
    if (order) {
      return {
        code: 0,
        data: order,
        message: '退款完成'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '订单不存在'
    }
  }
  
  if (url.match(/^\/orders\/\w+\/remark$/) && method.toLowerCase() === 'put') {
    const id = url.split('/')[2]
    const order = updateOrderRemarkById(id, data.remark)
    
    if (order) {
      return {
        code: 0,
        data: order,
        message: '备注更新成功'
      }
    }
    
    return {
      code: 404,
      data: null,
      message: '订单不存在'
    }
  }
  
  if (url === '/orders/export' && method.toLowerCase() === 'get') {
    // 模拟导出功能，返回一个模拟的Blob对象
    return {
      code: 0,
      data: new Blob(['模拟的订单导出数据'], { type: 'text/csv' }),
      message: '导出成功'
    }
  }
  
  // 默认返回404
  return {
    code: 404,
    data: null,
    message: '接口不存在'
  }
}

// 拦截请求并返回模拟数据
export function setupMock() {
  // 这里可以集成更复杂的模拟服务器，如MSW
  console.log('模拟服务已启用，登录账号: admin, 密码: 123456')
} 